package com.bear.cmcc.NetTestHTTP.ui.view;

import com.bear.cmcc.NetTestHTTP.NetworkTestApp;
import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.InfoSubBean;
import com.bear.cmcc.NetTestHTTP.util.InforSubUtil;

import android.content.Context;
import android.content.res.TypedArray;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * 频道九宫格
 * 
 * @author jqa
 * 
 */
public class ChannelsGridView extends ViewGroup {
	private static final String TAG = ChannelsGridView.class.getSimpleName();
	private Context mContext;
	private int row = 0;// 列
	private int line = 0;// 行
	private int mPaddingTop = 0;
	private List<InfoSubBean> channels;
	private int mControlWidth;
	private int mScreen;
	private float scale = 0;
	private int widthMeasureSpec;
	private int heightMeasureSpec;
	
	public List<InfoSubBean> getChannels() {
		return channels;
	}

	public void setChannels(List<InfoSubBean> channels) {
		this.channels = channels;
	}

	public ChannelsGridView(Context context) {
		super(context);
		this.mContext = context;
	}

	public ChannelsGridView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		this.mContext = context;
	}

	public ChannelsGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		TypedArray typeArray = mContext.obtainStyledAttributes(attrs,
				R.styleable.ChannelsGridView, defStyle, 0);
		row = typeArray.getInt(R.styleable.ChannelsGridView_row, 1);
		line = typeArray.getInt(R.styleable.ChannelsGridView_line, 1);

		typeArray.recycle();
		initView();
	}

	public void initView() {

		NetworkTestApp.loadChannelList(mContext,false);
		channels = NetworkTestApp.getChannelList();
		setClickable(true);
		// 总屏数,加1是最后有一个添加按钮
		
		this.mScreen = (int) Math.ceil(((double) channels.size() + 1)
				/ (row * line));// 计算页数Math.ceil(i)大于等于i的最小整数,Math.floor(i)小于等于i的最大整数

		// 初始化childView
		for (int i = 0; i < channels.size() + 1; i++) {
			{// 添加
				if (i == channels.size()) {
					View view = LayoutInflater.from(mContext).inflate(
							R.layout.channel_add, null);

					view.setFocusable(true);
					view.setClickable(true);
					addView(view);
				}else{
						InfoSubBean channel=channels.get(i);
						try {
							View view = LayoutInflater.from(mContext).inflate(
									R.layout.channel_item, null);
							
							TextView tvTitle = (TextView) view.findViewById(R.id.itemTitle);
							ImageView iv=(ImageView)view.findViewById(R.id.itemImg);
							TextView numberText = (TextView)view.findViewById(R.id.itemNumber);
							
							tvTitle.setText(channel.getTitle());
							iv.setImageBitmap(InforSubUtil.getImageFromAssetsFile(channel.getPath(), mContext));
							numberText.setText(""+channel.getNumber());
							view.setFocusable(true);
							view.setClickable(true);// 子视图要在父视图设置为true时才有效
							addView(view);// 减1是因为有一个添加操作的子视图
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				}
			}
		}
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {// 设置子视图

		if (changed) {
			final int count = getChildCount();
			int childLeft = l;
			int childTop = t + mPaddingTop;
			View view = null;
			int index = 0;
			// 屏,行,列
			A: for (int i = 0; i < mScreen; i++) {// 屏
				for (int j = 0; j < line; j++) {// 行
					for (int k = 0; k < row; k++) {// 列
						if (index >= count) {
							break A;
						}
						view = getChildAt(index);
						view.layout(
								childLeft
										+ (int) (view.getMeasuredWidth() * scale),
								childTop
										+ (int) (view.getMeasuredHeight() * scale),
								childLeft
										+ ((int) (view.getMeasuredWidth() * (1 - scale))),
								childTop
										+ ((int) (view.getMeasuredHeight() * (1 - scale))));

						childLeft += view.getMeasuredWidth();
						index++;
					}
					childLeft = mControlWidth * i;
					childTop = view.getMeasuredHeight() * (j + 1) + mPaddingTop;
				}
				childLeft = mControlWidth * (i + 1);
				childTop = t + mPaddingTop;
			}
			scale = 0;
		}

	}

	// 测量自身
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		this.widthMeasureSpec = widthMeasureSpec;
		this.heightMeasureSpec = heightMeasureSpec;
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);// 800
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);// 560
		mControlWidth = widthSize;
		final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize
				/ row, MeasureSpec.EXACTLY);
		final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
				heightSize / line, MeasureSpec.EXACTLY);

		final int count = getChildCount();
		for (int i = 0; i < count; i++) {
			getChildAt(i)
					.measure(childWidthMeasureSpec, childHeightMeasureSpec);

		}
		setMeasuredDimension(widthSize * mScreen, heightSize);

	}

	public void reSetLayout(float scale) {// 重新给子视图布局
		this.mScreen = (int) Math.ceil(((double) getChildCount())
				/ (row * line));// 重新计算页数
		onMeasure(widthMeasureSpec, heightMeasureSpec);// 重新计算父视图
		this.scale = scale;// 设置缩放尺寸
		onLayout(true, 0, 0, 0, 0);
	}

	public void addChannelView(InfoSubBean channel, OnClickListener listener) {// 添加一个新频道的视图

		try {
			View view = LayoutInflater.from(mContext).inflate(
					R.layout.channel_item, null);

			TextView tvTitle = (TextView) view.findViewById(R.id.itemTitle);
			tvTitle.setText(channel.getTitle());
			
			addView(view, getChildCount() - 1);// 减1是因为有一个添加操作的子视图
			view.setFocusable(true);
			view.setClickable(true);// 子视图要在父视图设置为true时才有效
			view.setOnClickListener(listener);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public int getmScreen() {// 获得总屏数
		return mScreen;
	}
}
